#include "CollectDeivceForm.h"
#include "ProtocalInterface.h"
#include "ui_CollectDeivceForm.h"
#include <QDebug>
#include <QDir>
#include <QPluginLoader>

CollectDeivceForm::CollectDeivceForm(QWidget* parent)
    : AttribBaseForm(parent)
    , ui(new Ui::CollectDeivceForm)
{
    ui->setupUi(this);

    ui->comboBox_2->setEditable(true);

    QWidget* tcpClientWidget = new QWidget();
    QVBoxLayout* vLayout = new QVBoxLayout();
    QHBoxLayout* hLayout = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("IP地址A");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrA", edit1);
        QLabel* label2 = new QLabel("端口号A");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("portA", edit2);
        hLayout->addWidget(label1);
        hLayout->addWidget(edit1);
        hLayout->addWidget(label2);
        hLayout->addWidget(edit2);
        vLayout->addLayout(hLayout);
    }
    QHBoxLayout* hLayout1 = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("IP地址B");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrB", edit1);
        QLabel* label2 = new QLabel("端口号B");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("portB", edit2);
        hLayout1->addWidget(label1);
        hLayout1->addWidget(edit1);
        hLayout1->addWidget(label2);
        hLayout1->addWidget(edit2);
        vLayout->addLayout(hLayout1);
    }
    vLayout->setMargin(0);
    tcpClientWidget->setLayout(vLayout);
    ui->stackedWidget->addWidget(tcpClientWidget);
    //tcpServer配置
    QWidget* tcpServerWidget = new QWidget();
    QHBoxLayout* hLayout3 = new QHBoxLayout();
    {
        QLabel* label1 = new QLabel("服务端IP地址");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("serverip", edit1);
        QLabel* label2 = new QLabel("服务端端口号");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("serverport", edit2);
        hLayout3->addWidget(label1);
        hLayout3->addWidget(edit1);
        hLayout3->addWidget(label2);
        hLayout3->addWidget(edit2);
    }
    hLayout3->setMargin(0);
    tcpServerWidget->setLayout(hLayout3);
    ui->stackedWidget->addWidget(tcpServerWidget);

    //UDP配置
    QWidget* udpWidget = new QWidget();
    QVBoxLayout* vLayout6 = new QVBoxLayout();
    {
        QHBoxLayout* hLayout4 = new QHBoxLayout();
        QHBoxLayout* hLayout5 = new QHBoxLayout();
        QLabel* label1 = new QLabel("远程IP");
        QLineEdit* edit1 = new QLineEdit();
        m_mapChilds.insert("updDestIpaddr", edit1);
        QLabel* label2 = new QLabel("远程端口");
        QLineEdit* edit2 = new QLineEdit();
        m_mapChilds.insert("updDestPort", edit2);
        QLabel* label3 = new QLabel("本地IP");
        QLineEdit* edit3 = new QLineEdit();
        m_mapChilds.insert("updSrcIpaddr", edit3);
        QLabel* label4 = new QLabel("本地端口");
        QLineEdit* edit4 = new QLineEdit();
        m_mapChilds.insert("updSrcPort", edit4);

        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        hLayout4->addWidget(label2);
        hLayout4->addWidget(edit2);

        hLayout5->addWidget(label3);
        hLayout5->addWidget(edit3);
        hLayout5->addWidget(label4);
        hLayout5->addWidget(edit4);

        vLayout6->addLayout(hLayout4);
        vLayout6->addLayout(hLayout5);
    }
    vLayout6->setMargin(0);
    udpWidget->setLayout(vLayout6);
    ui->stackedWidget->addWidget(udpWidget);
    //串口
    QWidget* commWidget = new QWidget();
    QVBoxLayout* vLayout7 = new QVBoxLayout();
    {
        QLabel* label1 = new QLabel("设备为串口时，使用通道上配置串口的参数");
        vLayout7->addWidget(label1);
    }
    vLayout7->setMargin(0);
    commWidget->setLayout(vLayout7);
    ui->stackedWidget->addWidget(commWidget);

    connectAllEdit();
}

CollectDeivceForm::~CollectDeivceForm()
{
    delete ui;
}

void CollectDeivceForm::InitUIData()
{
    if (ui->tabWidget->count() > 1) {
        ui->tabWidget->removeTab(1);
    }
    m_devInfo = ModelXml::GetInstance()->GetCollectDevicePointer(m_channelID, m_deviceID);
    if (m_devInfo) {
        ui->lineEdit->setText(m_devInfo->name.c_str());
        ui->lineEdit_2->setText(m_devInfo->desc.c_str());
        ui->comboBox_2->setCurrentText(m_devInfo->devAddr.c_str());
        ui->lineEdit_4->setText(m_devInfo->periodTime.c_str());
        ui->checkBox->setChecked(m_devInfo->isDisable);
        int n = m_devInfo->InterfaceType;
        ui->comboBox->setCurrentIndex((n));

        ((QLineEdit*)m_mapChilds["ipaddrA"])->setText(m_devInfo->tcpClient.ipaddrA.c_str());
        ((QLineEdit*)m_mapChilds["ipaddrB"])->setText(m_devInfo->tcpClient.ipaddrB.c_str());
        ((QLineEdit*)m_mapChilds["portA"])->setText(m_devInfo->tcpClient.portA.c_str());
        ((QLineEdit*)m_mapChilds["portB"])->setText(m_devInfo->tcpClient.portB.c_str());

        ((QLineEdit*)m_mapChilds["serverip"])->setText(m_devInfo->tcpServer.ipaddr.c_str());
        ((QLineEdit*)m_mapChilds["serverport"])->setText(m_devInfo->tcpServer.port.c_str());

        ((QLineEdit*)m_mapChilds["updDestIpaddr"])->setText(m_devInfo->udp.remoteIpaddr.c_str());
        ((QLineEdit*)m_mapChilds["updDestPort"])->setText(m_devInfo->udp.remotePort.c_str());
        ((QLineEdit*)m_mapChilds["updSrcIpaddr"])->setText(m_devInfo->udp.remoteIpaddr.c_str());
        ((QLineEdit*)m_mapChilds["updSrcPort"])->setText(m_devInfo->udp.remotePort.c_str());

        int row = 0;
        ui->tableWidget->setRowCount(m_devInfo->params.size());
        for (stPrivateParam st : m_devInfo->params) {
            ui->tableWidget->setItem(row, 0, new QTableWidgetItem(QString::number(st.id)));
            ui->tableWidget->setItem(row, 1, new QTableWidgetItem(st.name.c_str()));
            ui->tableWidget->setItem(row, 2, new QTableWidgetItem(st.desc.c_str()));
            ui->tableWidget->setItem(row, 3, new QTableWidgetItem(st.value.c_str()));
            row++;
        }
        //根据父通道，更改接口类型
        if (ModelXml::GetInstance()->GetCollectChannel(m_channelID).portType == stCollectChannel::Port_SerialPort) {
            if (ui->comboBox->count() < 4) {
                ui->comboBox->addItem("串口");
            }
            ui->comboBox->setCurrentIndex(3);
            ui->comboBox->setDisabled(true);
        } else {
            ui->comboBox->removeItem(3);
            ui->comboBox->setDisabled(false);
        }
    }
    QWidget* widget = GetProtocalConfigWidget(m_protoName);
    if (widget) {
        ui->tabWidget->addTab(widget, "设备点表");
        ui->tabWidget->setIconSize(QSize(20, 20));
        ui->tabWidget->setTabIcon(1, QIcon(":/resource/pointTable.png"));
        ui->tabWidget->setCurrentIndex(1);
    }
}

void CollectDeivceForm::saveModel()
{
    saveChanngeData();
    m_currentTreeItem->setText(0, "设备名称:" + ui->lineEdit->text());
}

stCollecDevice* CollectDeivceForm::saveChanngeData()
{

    m_devInfo->name = ui->lineEdit->text().toStdString();
    m_devInfo->desc = ui->lineEdit_2->text().toStdString();
    m_devInfo->devAddr = ui->comboBox_2->currentText().toStdString();
    m_devInfo->periodTime = ui->lineEdit_4->text().toStdString();
    m_devInfo->isDisable = ui->checkBox->checkState() == Qt::Checked ? true : false;
    m_devInfo->InterfaceType = (stCollecDevice::Intarface_Type)ui->comboBox->currentIndex();
    m_devInfo->tcpClient.ipaddrA = ((QLineEdit*)m_mapChilds.value("ipaddrA"))->text().toStdString();
    m_devInfo->tcpClient.ipaddrB = ((QLineEdit*)m_mapChilds.value("ipaddrB"))->text().toStdString();
    m_devInfo->tcpClient.portA = ((QLineEdit*)m_mapChilds.value("portA"))->text().toStdString();
    m_devInfo->tcpClient.portB = ((QLineEdit*)m_mapChilds.value("portB"))->text().toStdString();

    m_devInfo->tcpServer.ipaddr = ((QLineEdit*)m_mapChilds.value("serverip"))->text().toStdString();
    m_devInfo->tcpServer.port = ((QLineEdit*)m_mapChilds.value("serverip"))->text().toStdString();

    m_devInfo->udp.localIpaddr = ((QLineEdit*)m_mapChilds.value("updSrcIpaddr"))->text().toStdString();
    m_devInfo->udp.localPort = ((QLineEdit*)m_mapChilds.value("updSrcPort"))->text().toStdString();
    m_devInfo->udp.remoteIpaddr = ((QLineEdit*)m_mapChilds.value("updDestIpaddr"))->text().toStdString();
    m_devInfo->udp.remotePort = ((QLineEdit*)m_mapChilds.value("updDestPort"))->text().toStdString();

    m_devInfo->params.clear();
    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        stPrivateParam param;
        param.id = ui->tableWidget->item(i, 0)->text().toInt();
        param.name = ui->tableWidget->item(i, 1)->text().toStdString();
        param.desc = ui->tableWidget->item(i, 2)->text().toStdString();
        param.value = ui->tableWidget->item(i, 3)->text().toStdString();
        m_devInfo->params.push_back(param);
    }
    return m_devInfo;
}

void CollectDeivceForm::on_comboBox_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}

QWidget* CollectDeivceForm::GetProtocalConfigWidget(QString protocalName)
{
    QPluginLoader loader;
    QString pluginPath = QDir::currentPath() + "/Collect/" + protocalName + "/" + protocalName + "UI.dll";
    loader.setFileName(pluginPath);
    if (!loader.load()) {
        return nullptr;
    }
    QObject* plugin = loader.instance();
    qDebug() << __FUNCTION__ << loader.errorString();
    if (plugin == NULL) {
        qDebug("loader.instance() is null");
        return nullptr;
    }
    ProtocalInterface* pi = qobject_cast<ProtocalInterface*>(plugin);
    if (pi) {
        if (m_pointWidget) {
            delete m_pointWidget;
            m_pointWidget = nullptr;
        }
        m_pointWidget = pi->getWidget();
        if (m_devInfo->pointTable_node.empty())
            m_devInfo->createXML();
        pi->InitData(m_devInfo->pointTable_node);
        std::string str = m_devInfo->pointTable_node.name();
        str = m_devInfo->pointTable_node.child("tag").attribute("name").value();
        qDebug("Success2");
        if (m_pointWidget) {
            return m_pointWidget;
        }
    }
    return nullptr;
}
